package com.beimi.game.cmd;

import com.beimi.game.BaseGame;
import com.beimi.game.MJService;
import com.beimi.game.module.GamePlayerModule;
import com.beimi.game.module.GameTurnModule;
import com.beimi.game.player.GamePlayer;
import com.beimi.game.player.PlayerCardModule;
import com.beimi.game.proto.GuoInfo;
import com.beimi.net.AbstractGameCmd;
import com.next.core.util.GenericAnnotation;


@GenericAnnotation(type = "gang")
public class CmdGuo extends AbstractGameCmd {
	@Override
	public void execute(MJService service, GamePlayer player, String data) {
		BaseGame game = service.getGame();
		PlayerCardModule cardModule = player.getCardModule();

	    //如果玩家没有对应的操作，则也认为是非法消息
	    if(!cardModule.isCanGang() && !cardModule.isCanPeng() && !cardModule.isCanHu()){
	        return;
	    }

	    //如果是玩家自己的轮子，不是接牌，则不需要额外操作
	    boolean doNothing = game.getChuPai() == -1 && !(game.getModule(GameTurnModule.class).getCurrentTurn() == player.getSeat());

	    player.sendMessage("guo_result", null);
	    player.getCardModule().clearOptions();
	    
	    //这里还要处理过胡的情况
	    if(game.getChuPai() >= 0 && cardModule.isCanHu()){
	    	player.getCardModule().setGuoHuFan(cardModule.getTingMap().get(game.getChuPai()).getFan());
	    }

	    if(doNothing){
	        return;
	    }
	    
	    //如果还有人可以操作，则等待
	    if (game.getModule(GamePlayerModule.class).hasOperations()) {
			return;
		}

	    //如果是已打出的牌，则需要通知。
	    if(game.getChuPai() >= 0){
	    	GuoInfo guoInfo = new GuoInfo();
	    	guoInfo.setUserId(player.getUserId());
	    	guoInfo.setPai(game.getChuPai());
	        game.getModule(GamePlayerModule.class).broacastInRoom("guo_notify_push", guoInfo, player);
//	        seatData.folds.push(game.getChuPai());
	        game.setChuPai(-1);
	    }
	    
	    game.getModule(GamePlayerModule.class).clearOperations();
	    
//	    if(qiangGangContext != null && qiangGangContext.isValid){
//	        doGang(game,qiangGangContext.turnSeat, qiangGangContext.seatData, "wangang", 1, qiangGangContext.pai);        
//	    }
//	    else{
	        //下家摸牌
	    	game.getModule(GameTurnModule.class).incrTurn();
//	    	game.getModule(GameTurnModule.class).getPlayer().;
//	        doUserMoPai(game);   
//	    }
	}
}
